//turret.txt - Simple script for turrets
//Cell 0,1 - Stuff done flag. If both 0, nothing. Otherwise If non-zero, this 
//turret wont attack. If the turret is itself attacked, will set the not-attack setting back to 0.
//Cell 2,3 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.

begincreaturescript;

short i,target;
short create_count = 0;
short last_spawn,mess1 = 0,mess2 = 0;
short summon_mess = 0;
short spawn_stat = 0;
short targ;

body;

beginstate INIT_STATE;
	set_name(ME,"Ghaldring");

	set_level(ME,40);
	change_max_health(ME,400);
	set_boss_level(ME,2);
	place_particle_num(ME,18,12,10);

	last_spawn = get_current_tick();
	
	// reset test
	if (gf(60,9) == 4)
		sf(60,9,3);
		
	if (gf(100,23) > 0)
		erase_char(ME);
	if (gf(60,19) > 14)
		erase_char(ME);
	break;

beginstate DEAD_STATE;
	sf(101,10,1);
	begin_talk_mode(87);
break;

beginstate START_STATE; 
	if ((get_attitude(ME) < 10) && (gf(60,9) == 0) && (get_nearest_party_char(6) >= 0)) {
		talk_no_exit(63);
		}
	if ((get_attitude(ME) < 10) && (gf(60,9) == 3) && (get_nearest_party_char(6) >= 0)) {
		last_spawn = get_current_tick();
		talk_no_exit(77);
		}

	if (((gf(60,9) == 4) || (gf(60,9) == 5)) && (get_attitude(ME) < 10)) {
		if (spawn_stat == 0) {
			spawn_stat = 1;
			sf(60,9,5);
			activate_hidden_group(9);
			add_range_to_group(46,53,5);
			change_max_health(1005,200);
			place_particle_num(1005,10,7,10);
			set_level(1005,25);
			run_char_animation(2,1,50);	
			pc_heard_sound_delay(136,100);						
			
			last_spawn = get_current_tick();
			end();
			}
		
		if ((spawn_stat > 0) && (gf(60,9) == 5) && (num_chars_in_group(5) <= 4)) { // we win?
			sf(60,9,7);
			sf(60,21,1);
			begin_talk_mode(86);
			}
			
		if ((spawn_stat == 1) && (tick_difference(last_spawn,get_current_tick()) > 1)) { // pause before bless or kill
			last_spawn = get_current_tick();
			spawn_stat = 2;
			
			
			}
		if ((spawn_stat == 2) && (tick_difference(last_spawn,get_current_tick()) > 0)) { // bless or kill
			last_spawn = get_current_tick();
			
			// kill?
			targ = -1;
			if ((char_ok(46)) && (get_health(46) < get_max_health(46) / 2))
				targ = 46;
			else if ((char_ok(47)) && (get_health(47) < get_max_health(47) / 2))
				targ = 47;
			else if ((char_ok(48)) && (get_health(48) < get_max_health(48) / 2))
				targ = 48;
			else if ((char_ok(49)) && (get_health(49) < get_max_health(49) / 2))
				targ = 49;
			else if ((char_ok(50)) && (get_health(50) < get_max_health(50) / 2))
				targ = 50;
			else if ((char_ok(51)) && (get_health(51) < get_max_health(51) / 2))
				targ = 51;
			else if ((char_ok(52)) && (get_health(52) < get_max_health(52) / 2))
				targ = 52;
			else if ((char_ok(53)) && (get_health(53) < get_max_health(53) / 2))
				targ = 53;
			
			if (targ >= 0) {
				damage_char(targ,500 + get_ran(1,0,250),0);
				run_char_animation(2,1,50);	
				pc_heard_sound_delay(117,200);						
				print_named_str(ME,"punishes an inadequate creation.");
				
				if (mess1 == 0) {
					mess1 = 1;
					begin_talk_mode(84);
					}
				end();				
				}
			
			// bless?
			if (targ < 0) {
				if ((char_ok(46)) && (get_char_status(46,1) <= 0))
					targ = 46;
				else if ((char_ok(47)) && (get_char_status(47,1) <= 0))
					targ = 47;
				else if ((char_ok(48)) && (get_char_status(48,1) <= 0))
					targ = 48;
				else if ((char_ok(49)) && (get_char_status(49,1) <= 0))
					targ = 49;
				else if ((char_ok(50)) && (get_char_status(50,1) <= 0))
					targ = 50;
				else if ((char_ok(51)) && (get_char_status(51,1) <= 0))
					targ = 51;
				else if ((char_ok(52)) && (get_char_status(52,1) <= 0))
					targ = 52;
				else if ((char_ok(53)) && (get_char_status(53,1) <= 0))
					targ = 53;

				if (targ >= 0) {
					set_char_status(targ,0,20);
					set_char_status(targ,1,20);
					set_char_status(targ,8,20);
					bless_char(targ,2);
					run_char_animation(2,1,50);	
					pc_heard_sound_delay(100,200);						
					print_named_str(ME,"blesses a creation.");
					
					if (mess2 == 0) {
						mess2 = 1;
						begin_talk_mode(85);
						}
					end();				
					}


				}
			}
			
		}
		
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		last_spawn = get_current_tick();
		do_attack();
		set_state(3);
		}

		
	if (get_foe_target(ME,10,0)) {
		last_spawn = get_current_tick();
		do_attack();
		set_state(3);
		}
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking

		
	if (target_ok() == FALSE)
		set_state(START_STATE);
	

			
	if ((get_attitude(ME) >= 10) && (char_ok(27) == FALSE)) {
		print_named_str(ME,"calls forth a creation!");
		spawn_creature(19);
		set_summon_level(27,1);
		place_particle_num(27,10,7,10);
		run_char_animation(2,1,50);	
		pc_heard_sound_delay(136,100);						
		last_spawn = get_current_tick();
		end();
		}
	if ((get_attitude(ME) >= 10) && (char_ok(28) == FALSE)) {
		print_named_str(ME,"calls forth a creation!");
		spawn_creature(20);
		set_summon_level(28,1);
		place_particle_num(28,10,7,10);
		run_char_animation(2,1,50);	
		pc_heard_sound_delay(136,100);						
		last_spawn = get_current_tick();
		end();
		}

	do_attack();

	turret_heal();
break;

beginstate TALKING_STATE;
	if (gf(60,9) < 3)
		begin_talk_mode(63);
	if (gf(60,9) == 3)
		begin_talk_mode(77);
	if ((gf(60,9) == 4) || (gf(60,9) == 5))
		print_str_color("Talking: Ghaldring doesn't want to talk while he's testing you.",2);
	if (gf(60,9) > 5)
		begin_talk_mode(90);
break;
